using PureMVC.Patterns.Observer;
using System;
using UnityEngine;


namespace WooEngine.Common
{

    public abstract class Singleton<T> where T: Notifier, new()
    {

#if ODIN_INSPECTOR
        [SerializeField]
        protected bool showOdinInfo;
#endif

        private static T _instance;
        private static readonly object _lock = new object();

        public static T Instance
        {
            get
            {

                if (applicationIsQuitting)
                {
                    Debug.LogWarning("[Singleton] Instance '" + typeof(T) +
                        "' already destroyed on application quit." +
                        " Won't create again - returning null.");
                    return null;
                }

                // Double-Checked Locking
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        _instance = (default(T) != null) ? default(T) : Activator.CreateInstance<T>();
                    }
                }

                return _instance;
            }
        }

        public static bool applicationIsQuitting = false;

        public void OnDestroy()
        {
            applicationIsQuitting = true;

            OnDelete();
            _instance = null;
        }

        protected abstract void OnDelete();
    }
}
